{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import special\n",
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Q_func(x):\n",
    "    return 0.5 * special.erfc(x/(np.sqrt(2)))\n",
    "\n",
    "def Q_inv(x):\n",
    "    return np.sqrt(2) * special.erfcinv(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_pfs():\n",
    "    model = 'DetectNet'\n",
    "    mod = 'GFSK'\n",
    "    sample_length = [64,128,256,512,1024]\n",
    "\n",
    "    pfs = []\n",
    "    for _sample_length in sample_length:\n",
    "        doc = pd.read_csv('./result/xls/'+model+'/'+str(_sample_length)+'/'+mod+'Pds.xls', header=None)\n",
    "        line = np.array(doc)\n",
    "        pfs.append(line[0,40])\n",
    "    return pfs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pf:  [6.611468116658428, 7.409729187562688, 5.899084837991589, 6.3289540171803935, 7.337735143889374]\n",
      "Tamanho amostra:  64  SNR-wall (db):  -3.674300016072613\n",
      "Tamanho amostra:  128  SNR-wall (db):  -5.584559361948055\n",
      "Tamanho amostra:  256  SNR-wall (db):  -7.054080471561901\n",
      "Tamanho amostra:  512  SNR-wall (db):  -8.756803851694588\n",
      "Tamanho amostra:  1024  SNR-wall (db):  -10.4841298817222\n"
     ]
    }
   ],
   "source": [
    "Q_pd =  Q_inv(0.90)\n",
    "\n",
    "Ns = [64,128,256,512,1024]\n",
    "pfs = get_pfs()\n",
    "print(\"Pf: \", pfs)\n",
    "\n",
    "for i in range(5):\n",
    "    N = Ns[i]\n",
    "    \n",
    "    Q_pf =  Q_inv(pfs[i]/100)\n",
    "    \n",
    "    sqrt_fi = np.sqrt(1/N)\n",
    "    snrWall = ((1-Q_pd * sqrt_fi)/(1-Q_pf * sqrt_fi))-1\n",
    "\n",
    "    print(\"Tamanho amostra: \",N, \" SNR-wall (db): \", 10 * np.log10(snrWall))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def snr_wall(list_pd):\n",
    "    snr = np.linspace(-20,19,40)\n",
    "    for i in range(len(list_pd)):\n",
    "        pd = list_pd[i]\n",
    "        if(pd >= 0.9):\n",
    "            return ((0.9-list_pd[i-1])/(pd-list_pd[i-1])) + snr[i-1] \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tamanho amostra:  64  SNR-wall (db):  -8.018371212121211\n",
      "Tamanho amostra:  128  SNR-wall (db):  -9.323159784560143\n",
      "Tamanho amostra:  256  SNR-wall (db):  -10.231570109151972\n",
      "Tamanho amostra:  512  SNR-wall (db):  -10.661096605744126\n",
      "Tamanho amostra:  1024  SNR-wall (db):  -11.383970358814352\n"
     ]
    }
   ],
   "source": [
    "\n",
    "model = 'DetectNet'\n",
    "mod = 'GFSK'\n",
    "sample_length = [64,128,256,512,1024]\n",
    "\n",
    "results = []\n",
    "pf = []\n",
    "samples = []\n",
    "for _sample_length in sample_length:\n",
    "    #print('result/xls/'+model+'/'+str(_sample_length)+'/'+mod+'Pds.xls')\n",
    "    doc = pd.read_csv('./result/xls/'+model+'/'+str(_sample_length)+'/'+mod+'Pds.xls', header=None)\n",
    "    line = np.array(doc)\n",
    "    line = line[0,range(40)]\n",
    "    print(\"Tamanho amostra: \",_sample_length, \" SNR-wall (db): \", snr_wall(line))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
